package cn.rubintry.bug.common.base;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Method;

import cn.rubintry.bug.common.base.event.NetWorkFailedEvent;

public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {

    protected T binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this , true);
        Class<? extends ViewBinding> bindingType = bindingClazz();
        try {
            //反射调用bind，获取viewBinding
            Method bindingMethod = bindingType.getDeclaredMethod("inflate" , LayoutInflater.class);
            binding = (T) bindingMethod.invoke(this , getLayoutInflater());
        }catch (Exception ignored){

        }
        setContentView(binding.getRoot());
        getActivityParams();
        initView();
        observeData();
    }



    public abstract  Class<T> bindingClazz();


    public abstract void getActivityParams();


    public abstract void initView();

    public abstract void observeData();


    protected abstract void onNetWorkError(String msg);

    @Subscribe(threadMode = ThreadMode.MAIN)
    private void  netWorkError(NetWorkFailedEvent event){
        ToastUtils.showShort(event.getMsg());
        onNetWorkError(event.getMsg());
    }

    @Override
    protected void onDestroy() {
        ToastUtils.cancel();
        super.onDestroy();
    }
}
